<template>
  <div>
    <el-dialog
      width="700px"
      :visible.sync="isShow"
      title="分配角色"
      @close="closeEvent"
    >
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
          item.name
        }}</el-checkbox>
      </el-checkbox-group>
      <template #footer>
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// 导入获取角色接口
import { sysRole } from '@/api/setting'
// 导入获取当前用户角色信息接口
import { sysUser } from '@/api/user'
// 导入分配角色接口
import { sysUserAssignRoles } from '@/api/employees'
export default {
  data() {
    return {
      isShow: false,
      roleIds: [], // 当前用户的角色数据
      list: [], // 角色列表
      Id: null // 当前用户id
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取角色函数
    async getData() {
      const res = await sysRole()
      this.list = res.data.rows
    },

    // 获取当前用户角色信息
    async getUserRole(id) {
      const res = await sysUser(id)
      this.roleIds = res.data.roleIds
      this.Id = id
    },

    // 点击确定按钮点击事件
    async submit() {
      this.isShow = false
      await sysUserAssignRoles({ id: this.Id, roleIds: this.roleIds })
      this.$message.success('分配角色成功')
    },

    // 关闭事件
    closeEvent() {
      this.roleIds = []
    }
  }
}
</script>

<style></style>
